package com.pingbu.tijian.hospital.backapi.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pingbu.common.bean.ResultBean;
import com.pingbu.common.utils.PbFileUtils;
import com.pingbu.common.utils.PbSecretUtils;
import com.pingbu.tijian.hospital.backapi.bean.entity.*;
import com.pingbu.tijian.hospital.backapi.bean.vo.FullTableStudentZuoye;
import com.pingbu.tijian.hospital.backapi.config.GlobalConst;
import com.pingbu.tijian.hospital.backapi.config.GlobalSet;
import com.pingbu.tijian.hospital.backapi.mapper.*;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 信息管理业务类，暂时无用
 */
@Service
public class MessageManagerService {

    private static Logger logger = LoggerFactory.getLogger(MessageManagerService.class);

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private GlobalSet globalSet;
    @Autowired
    private UserPermissionMapper userPermissionMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentZuoyeMapper studentZuoyeMapper;
    @Autowired
    private RevertMapper revertMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    /**
     * 评论分页
     * @param param
     * @return
     */
    public ResultBean<PageInfo<T>> page(Revert param) {
        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        //获取所有回复的信息
        List<Revert> lst1 = revertMapper.list(param);
        Comment comment = new Comment();
        //循环设置属性
        for (Revert revert : lst1) {
            //得到回复的是哪条评论对象
            comment.setNoid(revert.getComment_noid());
            Comment c2 = commentMapper.get(comment);
            //设置评论和回复的作业id
            revert.setComment_zuoye_noid(c2.getComment_zuoye_noid());
            revert.setStr_revert_date(sdf.format(revert.getRevert_date()));
        }
        PageInfo<Revert> pi1 = new PageInfo<>(lst1);
        return ResultBean.create(0,"success",pi1);
    }

    /**
     * 评论分页
     * @param param
     * @return
     */
    public ResultBean<PageInfo<T>> page(Comment param) {
        List<Comment> lst1 = new ArrayList<>();
        UserInfo u1 = new UserInfo();
        u1.setNoid(param.getUser_noid());
        u1 = userInfoMapper.get(u1);
        //根据user对象中的usercode获取学生信息
        Student stu1 = new Student();
        stu1.setBind_usercode(u1.getUsercode());
        //根据usercode查询学生信息
        stu1 = studentMapper.get(stu1);
        //根据学生noid获取到学生所有的作业信息
        StudentZuoye studentZuoye = new StudentZuoye();
        studentZuoye.setStudent_id(stu1.getNoid());
        List<StudentZuoye> stuZuoyeList = studentZuoyeMapper.list(studentZuoye);
        Comment c1 = new Comment();


        for (StudentZuoye zuoye : stuZuoyeList) {
            c1.setComment_zuoye_noid(zuoye.getNoid());
            List<Comment> lst2 = commentMapper.list(c1);
            if (lst2 != null && lst2.size() > 0) {
                lst1.add(lst2.get(0));
            }
        }

        //设置时间,格式为：yyyy-MM-dd HH:mm
        for (Comment comment : lst1) {
            comment.setStr_comment_date(sdf.format(comment.getComment_date()));
        }
        PageInfo<Comment> pi1 = startPage(lst1, param.getPageNum(), param.getPageSize());
        return ResultBean.create(0,"success",pi1);
//        PageHelper.startPage(param.getPageNum(),param.getPageSize());
    }

    /**
     * 对list集合进行分页
     * @param list
     * @param pageNum
     * @param pageSize
     * @param <T>
     * @return
     */
    public <T> PageInfo<T> startPage(List<T> list, Integer pageNum, Integer pageSize) {
        //创建Page类
        com.github.pagehelper.Page page = new com.github.pagehelper.Page(pageNum, pageSize);
        //为Page类中的total属性赋值
        page.setTotal(list.size());
        //计算当前需要显示的数据下标起始值
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, list.size());
        //从链表中截取需要显示的子链表，并加入到Page
        page.addAll(list.subList(startIndex,endIndex));
        //以Page创建PageInfo
        PageInfo pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    /**
     * 删除一条消息
     * @param param
     * @return
     */
    public ResultBean<Integer> delete(Comment param) {
        //如果是管理员
        Integer i = 0;
        if (param.getUser_noid().equals("1")) {
            Revert revert = new Revert();
            revert.setNoid(param.getNoid());
            i = revertMapper.delete(revert);
        } else {
            i = commentMapper.delete(param);
        }
        return ResultBean.create(0, "success", i);
    }
}
